import os
import wx
import wx.lib.agw.aui as aui
import wx.html
import rwb
import firebrick

FID = "rwb.panel.welcome"

class WelcomePanel(firebrick.BasePanel):
    name = "welcome"
    _id = FID

    def __init__(self, parent):
        self.window = parent
        super(WelcomePanel, self).__init__(parent)
#        self._html = wx.html.HtmlWindow(self)
#        import pdb ; pdb.set_trace()
        self._html = CopyableHtmlWindow(self, wx.ID_ANY)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self._html, 1, wx.EXPAND)
        self.SetSizer(sizer)

        parent.auimgr.AddPane(self, aui.AuiPaneInfo().Center().
                              CaptionVisible(True).
                              Caption("Welcome").
                              BestSize(wx.Size(300,600)).
                              Name(FID).Show(False))
        self._html.SetPage(CONTENT)

    def OnLinkClicked(self, event):
        '''Handle clicks on links'''
        if event.Href == "view/browse_mode":
            self.window.CreateDefaultPerspective("Browse Mode")
        elif  event.Href == "view/edit_mode":
            self.window.CreateDefaultPerspective("Edit Mode")

class CopyableHtmlWindow(wx.html.HtmlWindow):
    '''An HTML window that allows copying'''
    def __init__(self, parent, id, *args, **kwargs):
        super(CopyableHtmlWindow, self).__init__(parent, id, *args, **kwargs)
        self.parent = parent

    def Copy(self):
        return self.SelectionToText()

    def CanCopy(self):
        if len(self.SelectionToText()) > 0:
            return True
        return False
    
    def OnLinkClicked(self, event):
        self.parent.OnLinkClicked(event)

# what would be bitchin' is if I could put links in here
# like <a href="menubar://perspective/NewPerspective">...</a>
CONTENT = '''
<html>
<body>
<div align="center">
<font size=+1><i>Welcome to the</i></font><br></font>
<font color="#b22222" size=+3><b><i>Robot Framework Workbench</i></b></font>
</div>

<p>This is a <i>pre-pre-pre</i> alpha release of the workbench
with limited functionality.<p>
</p>
<p>The workbench provides two ways to work with robot test suites:
<i>browse mode</i> and <i>edit mode</i>. You can switch between
these modes using the <b>View</b> menu.
</p>
<p>
<li><a href="view/edit_mode">Edit mode</a> provides the same navigation window on the left,
but provides a notebook on the right in which you can open up one 
or more test cases by double clicking over an item in the navigation
window. 
</p>
<p>
<a href="view/browse_mode">Browse mode</a> has a navigation window that shows the test suite
hierarchy. Single-clicking over a test case or suite displays that
suite on the view panel on the right. Double-clicking on an item
will open up the edit mode nodebook so that you can edit the test case.
</p>
<p>
Clicking on one of the above links will open that view, or you can 
use the View menu.
</p>
</body>
</html>
'''

